package com.zoweunion.mechanic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zoweunion.mechanic.dao.TSSsServerBrandParentDao;
import com.zoweunion.mechanic.dao.TSSsVehicleBrandDao;
import com.zoweunion.mechanic.dao.TSSsVehicleBrandParentDao;
import com.zoweunion.mechanic.dao.TSSsVehicleModelDao;
import com.zoweunion.mechanic.entity.TSSsServerBrandParentEntity;
import com.zoweunion.mechanic.entity.TSSsVehicleBrandEntity;
import com.zoweunion.mechanic.entity.TSSsVehicleBrandParentEntity;
import com.zoweunion.mechanic.entity.TSSsVehicleModelEntity;
import com.zoweunion.mechanic.service.TSSsVehicleBrandParentService;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.Orgin;
import com.zoweunion.mechanic.util.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("tSSsVehicleBrandParentService")
public class TSSsVehicleBrandParentServiceImpl extends ServiceImpl<TSSsVehicleBrandParentDao, TSSsVehicleBrandParentEntity> implements TSSsVehicleBrandParentService {
    public static int CODE_DIFFERENT_PAGE = 77777;// 请使用原始页面

    @Autowired
    private TSSsVehicleBrandParentDao tSSsVehicleBrandParentDao;
    @Autowired
    private TSSsServerBrandParentDao tSSsServerBrandParentDao;
    @Autowired
    private TSSsVehicleBrandDao tSSsVehicleBrandDao;
    @Autowired
    private TSSsVehicleModelDao tSSsVehicleModelDao;

    @Override
    public List<TSSsVehicleBrandParentEntity> tSSsVehicleBrandParentList(Map<String, Object> params) throws MyException {
        List<TSSsServerBrandParentEntity> list = this.tSSsVehicleBrandParentListFor(params);
        List<TSSsVehicleBrandParentEntity> list1 = new ArrayList<>();
        if (list.size() > 0 ) {
            list.forEach(item -> {
                LambdaQueryWrapper<TSSsVehicleBrandParentEntity> lambdaQueryWrapper1 = Wrappers.<TSSsVehicleBrandParentEntity>lambdaQuery();
                //机型品牌搜索判断，如果是搜索则参数brandName是存在的，则走else逻辑
                if(params.get("brandName") == null){
                    lambdaQueryWrapper1.eq(true, TSSsVehicleBrandParentEntity::getId, item.getParentBrandId()).eq(true, TSSsVehicleBrandParentEntity::getDeleteFlag, 0)
                            .orderByAsc(TSSsVehicleBrandParentEntity::getSortNum);
                }else{
                    lambdaQueryWrapper1.eq(true, TSSsVehicleBrandParentEntity::getId, item.getParentBrandId()).eq(true, TSSsVehicleBrandParentEntity::getDeleteFlag, 0)
                            .like(true,TSSsVehicleBrandParentEntity::getBrandParentName,params.get("brandName").toString())
                            .orderByAsc(TSSsVehicleBrandParentEntity::getSortNum);
                }
                list1.addAll(tSSsVehicleBrandParentDao.selectList(lambdaQueryWrapper1));
            });
            list1.sort(Comparator.comparingInt(TSSsVehicleBrandParentEntity::getSortNum));
        } else {
            LambdaQueryWrapper<TSSsVehicleBrandParentEntity> lambdaQueryWrapper1 = Wrappers.<TSSsVehicleBrandParentEntity>lambdaQuery().orderByAsc(TSSsVehicleBrandParentEntity::getSortNum);
            lambdaQueryWrapper1.eq(true, TSSsVehicleBrandParentEntity::getDeleteFlag, 0);
            list1.addAll(tSSsVehicleBrandParentDao.selectList(lambdaQueryWrapper1));
        }
        return list1;
    }

    public List<TSSsServerBrandParentEntity> tSSsVehicleBrandParentListFor(Map<String, Object> params) {
        LambdaQueryWrapper<TSSsServerBrandParentEntity> lambdaQueryWrapper = Wrappers.<TSSsServerBrandParentEntity>lambdaQuery();
        lambdaQueryWrapper.eq(true, TSSsServerBrandParentEntity::getServerId, params.get("sId").toString()).eq(true, TSSsServerBrandParentEntity::getDeleteFlag, 0);
        List<TSSsServerBrandParentEntity> list = tSSsServerBrandParentDao.selectList(lambdaQueryWrapper);
        return list;
    }

    @Override
    public List<TSSsVehicleBrandEntity> tSSsVehicleBrandList(Map<String, Object> params) throws MyException {
        LambdaQueryWrapper<TSSsVehicleBrandEntity> lambdaQueryWrapper = Wrappers.<TSSsVehicleBrandEntity>lambdaQuery();
        //机型品牌搜索判断，如果是搜索则参数brandName是存在的，则走else逻辑
        if(params.get("brandName") == null) {
            lambdaQueryWrapper.eq(true, TSSsVehicleBrandEntity::getParentId, params.get("ParentBrandId").toString());
        }else{
            lambdaQueryWrapper.like(true, TSSsVehicleBrandEntity::getBrandName, params.get("brandName").toString());
        }
        List<TSSsVehicleBrandEntity> list = tSSsVehicleBrandDao.selectList(lambdaQueryWrapper);
        if (list.size() > 0 || params.get("brandName") != null) {
            return list;
        } else {
            throw new MyException(Constants.CODE_HANDEL_FAILED, "该父级品牌下不存在子品牌,请联系客服");
        }
    }

    @Override
    public List<TSSsVehicleModelEntity> tSSsVehicleModelList(Map<String, Object> params) throws MyException {
        LambdaQueryWrapper<TSSsVehicleModelEntity> lambdaQueryWrapper = Wrappers.<TSSsVehicleModelEntity>lambdaQuery();
        //机型品牌搜索判断，如果是搜索则参数brandName是存在的，则走else逻辑
        if(params.get("brandName") == null) {
            lambdaQueryWrapper.eq(true, TSSsVehicleModelEntity::getBrandId, params.get("BrandId").toString()).eq(true, TSSsVehicleModelEntity::getDeleteFlag, 0);
        }else {
            lambdaQueryWrapper.like(true, TSSsVehicleModelEntity::getModelName, params.get("brandName").toString()).eq(true, TSSsVehicleModelEntity::getDeleteFlag, 0);
        }
        List<TSSsVehicleModelEntity> list = tSSsVehicleModelDao.selectList(lambdaQueryWrapper);

        if (list.size() > 0 || params.get("brandName") != null) {
            return list;
        } else {
            throw new MyException(Constants.CODE_HANDEL_FAILED, "该类型下不存在型号，请联系客服");
        }
    }
}
